<?php
/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 */

/**
 * Class with functions to show SharePoint lists inserted from the
 * Camelot .NET Connector
 *
 * @author Bendsoft
 */
class SharePointListReader extends CamelotList {

    private $_list_id = null;
    private $_list_status = null;

    /**
     * Function to get a single list schema
     *
     * @access	public
     * @param int|string $id Required, if stated as int it selects the camelot_list_id, if string it selects the camelot_list_id_external (SharePoint Guid)
     * @param int $status Can be 1, 0 or not stated. 1 means currently active, 0 is all inactive and if not stated ($status = '') all items are selected
     * @return object
     */
    function get_schema($id, $status = 1) {

        $this->_list_id = $id;
        $this->_list_status = $status;

        // Get the where clause
        $where = $this->selection_where_clause();

        /**
         * Open records from the table camelot_list
         * If returning 0 no records where found
         */
        parent::open($where);

        return $this;
    }

    /**
     * Function to show lists with some preselected data
     *
     * @access	public
     * @param int|string $id Optional, if stated as int it selects the camelot_list_id, if string it selects the camelot_list_id_external (SharePoint Guid)
     * @param int $status Can be 1, 0 or not stated (null). 1 means currently active, 0 is all inactive and if not stated ($status = '') all items are selected
     * @return object
     */
    function show_lists($id = null, $status = 1) {

        $this->_list_id = $id;
        $this->_list_status = $status;

        // Get the where clause
        $where = $this->selection_where_clause();

        /**
         * Open records from the table camelot_list
         * If returning 0 no records where found
         */
        $lists = parent::open($where);

        // If the data selection fails we should return an error message
        if($lists == 0) {
            // construct object
            $data->_headers = array('Error');
            $data->_content = array(
                0 => array('An error occured!'),
                1 => array($where)
                );
            return $data;
        }

        $data->_headers = array(
            "ID",
            "Title",
            "GUID",
            "Created",
            "Modified",
            "Last deleted",
            "Items",
            "Status"
        );

        $temp = array();
        foreach ($lists as $key => $value) {

            $item->camelot_list_id = $value['camelot_list_id'];
            $item->camelot_list_title = $value['camelot_list_title'];
            $item->camelot_list_id_external = $value['camelot_list_id_external'];
            $item->camelot_list_date_created = $value['camelot_list_date_created'];
            $item->camelot_list_date_modified = $value['camelot_list_date_modified'];
            $item->camelot_list_date_lastdeleted = $value['camelot_list_date_lastdeleted'];
            $item->camelot_list_itemcount = $value['camelot_list_itemcount'];
            $item->camelot_list_status = $value['camelot_list_status'];

            $temp[] = (array) $item;
        }

        $data->_content = $temp;

        return $data;
    }

    /**
     * Function to show lists as simple html table with some preselected fields
     *
     * @access	public
     * @param int|string $id Optional, if stated as int it selects the camelot_list_id, if string it selects the camelot_list_id_external (SharePoint Guid)
     * @param int $status Can be 1, 0 or not stated (null). 1 means currently active, 0 is all inactive and if not stated ($status = '') all items are selected
     * @return string
     */
    function show_lists_as_table($id = null, $status = 1) {

        $this->_list_id = $id;
        $this->_list_status = $status;

        // Get the where clause
        $where = $this->selection_where_clause();

        /**
         * Open records from the table camelot_list
         * If returning 0 no records where found
         */
        $lists = parent::open($where);

        $table = "
        <table id=\"listTable\" class=\"tablesorter\" border=\"0\">
            <thead>
                  <tr>
                        <th>ID</th>
                        <th>Title</th>
                        <th>GUID</th>
                        <th>Created</th>
                        <th>Modified</th>
                        <th>Last deleted</th>
                        <th>Items</th>
                  </tr>
            </thead>
            <tbody>";

        foreach ($lists as $key => $value) {
            $table .= "
	  <tr>
		<td>$value[camelot_list_id]</td>
		<td>$value[camelot_list_title]</td>
		<td>$value[camelot_list_id_external]</td>
		<td>$value[camelot_list_date_created]</td>
		<td>$value[camelot_list_date_modified]</td>
		<td>$value[camelot_list_date_lastdeleted]</td>
		<td>$value[camelot_list_itemcount]</td>
	  </tr>";
        }
        $table .= '</tbody></table>';

        return $table;
    }

    /**
     * Function to build the where clause for list selection. All properties are
     * set in private variables
     *
     * @access	private
     * @return string
     */
    private function selection_where_clause() {

        $where = '';

        // Validate and handle $id
        switch (gettype($this->_list_id)) {
            // Do nothing if null
            case NULL:
                break;

            // If string it should be a Guid
            case 'string':
                if ($this->validate_guid($this->_list_id)) {
                    $where .= " WHERE camelot_list_id_external = '$this->_list_id' ";
                } else {
                    return 'msg:300';
                }
                break;

            // If snteger it's a single id
            case 'integer':
                $where .= " WHERE camelot_list_id = '$this->_list_id' ";
                break;
        }

        // Validate and handle $status
        switch ($this->_list_status) {
            // if 1 or 0
            case 0:
            case 1:
                if (is_int($this->_list_status))
                    $where = ($where) ? $where .= " AND camelot_list_status = '$this->_list_status' " : " WHERE camelot_list_status = '$this->_list_status' ";
                break;
        }

        return $where;
    }

    /**
     * Function to validate if Sharepoint Guid is correct.
     * SharePoint Guid's should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
     *
     * @access	private
     * @param string $guid
     * @return bool
     */
    private function validate_guid($guid) {

        return eregi("^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$", $guid);
    }

}

/**
 * Class with functions to show SharePoint content inserted with
 * Camelot .NET Connector
 *
 * @author Bendsoft
 */
class SharePointContentReader extends CamelotContent {

    private $_list_id = null;
    private $_list_status = null;
    private $_columns = null;

    /**
     * Method to get list data
     *
     * @access public
     * @param int|string $id Required, if stated as int it selects the camelot_list_id, if string it selects the camelot_list_id_external (SharePoint Guid)
     * @param int $status Can be 1, 0 or not stated. 1 means currently active, 0 is all inactive and if not stated ($status = '') all items are selected
     * @param string $columns What columns to show, semicolon separated attribute_name. Default shows everything.
     * @return object with data
     */
    function get_list_data($id, $status = 1, $colums = null) {

        if (!$id)
            die('msg:303');

        $this->_list_id = $id;
        $this->_list_status = $status;

        // split columns (if any) into array
        if ($colums) {
            $this->_columns = explode(";", $colums);
        } else {
            // set something to prevent warning when loading schema
            $this->_columns = null;
        }

        // Load the schema
        $schema = $this->get_schema($this->_list_id, $this->_list_status);

        // Load the list content
        $content = $this->get_content($schema->camelot_list_id);

        // Create CamelotXmlSorting instance
        $CamelotXmlSorting = new CamelotXmlSorting();

        // Create the schema sorter
        $this->_sorted->_schema = $CamelotXmlSorting->sort_schema($schema->camelot_list_schema, $this->_columns);

        // Sort the list content
        $this->_sorted->_content = $CamelotXmlSorting->sort_content($content, $this->_sorted->_schema->attribute_name);

        // Kill CamelotXmlSorting instance
        unset ($CamelotXmlSorting);

        return $this->_sorted;
    }

    /**
     * Method to get the Schema
     *
     * @access private
     * @return object with data
     *
     * @example
     * [camelot_list_id] => 30
     * [camelot_list_id_external] => a436e57e-5274-4375-a7fa-767f1160a52c
     * [camelot_list_title] => Content
     * [camelot_list_description] => Website content
     * [camelot_list_schema] => <Schema>...</schema>
     * [camelot_list_date_modified] => 2010-05-18 03:23:14
     * [camelot_list_date_lastdeleted] => 2010-05-14 05:19:29
     * [camelot_list_date_created] => 2010-05-14 05:19:29
     * [camelot_list_itemcount] => 7
     * [camelot_list_ip] => 192.168.1.2
     * [camelot_list_status] => 1
     */
    private function get_schema() {

        if (!$this->_list_id)
            die('msg:303');

        $SharePointList = new SharePointListReader();
        
        return $SharePointList->get_schema($this->_list_id, $this->_list_status);
    }

    /**
     * Method to get the List data
     *
     * @access private
     * @param internal list id
     * @return array with content xml
     */
    private function get_content($listid) {

        $result = parent::open(" WHERE camelot_content_camelot_list_id = '$listid'");

        if ($result == 0)
            die('msg:304');

        // create new array container
        $data = array();
        foreach ($result as $key => $value) {
            $data[] = $value['camelot_content_content'];
        }

        return $data;
    }

}

?>
